package com.hznu.jspFinal.service;

import com.hznu.jspFinal.dao.AttendExamMapper;
import com.hznu.jspFinal.dao.CourseStudentMapper;
import com.hznu.jspFinal.dao.ExamMapper;
import com.hznu.jspFinal.entity.AttendExam;
import com.hznu.jspFinal.entity.CourseStudent;
import com.hznu.jspFinal.entity.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jamesZhan
 */
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private AttendExamMapper attendExamMapper;

    @Autowired
    private CourseStudentMapper courseStudentMapper;

    /**
     * author:wp
     * time:22:49 2018/06/17
     * @param exam
     * @return
     */
    @Override
    public void add(Exam exam) {


        //新创建的考试状态默认设置为 未开始(1)
        exam.setState(1);

        List<CourseStudent> courseStudents = courseStudentMapper.selectByCourseTeacherid(exam.getCourseteachid());
        //参加考试人数 = 教学班学生数
        exam.setNop(courseStudents.size());

        //先insert 才能获取id
        examMapper.insertSelective(exam);

        //给这个教学班的所有学生创建attendExam记录
        for(CourseStudent courseStudent :courseStudents){
            AttendExam attendExam = new AttendExam();
            attendExam.setStudentid(courseStudent.getStudentid());
            attendExam.setExamid(exam.getId());

            //attendExam其他的属性要等考试进行和结束之后才有
            attendExamMapper.insertSelective(attendExam);
        }

    }

    @Override
    public Exam findByExamId(Integer id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKeySelective(exam);
    }

    /**
     * author:wp
     * time: 23:01 2018/06/17
     * @param id
     * @return
     */
    @Override
    public void delete(Integer id) {

        //删除attendExam表中该场考试的记录
        attendExamMapper.deleteByExamId(id);

        //删除exam表中记录
        examMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Exam findByName(String name) {
        return examMapper.selectByName(name);
    }

    @Override
    public List<Exam> selectByCourseId(Integer courseid) {
        return examMapper.selectByCourseId(courseid);
    }

    /**
     * author:wp
     * time:11:32 2018/06/18
     * @param studentid
     * @return
     */
    @Override
    public List<Exam> findByStudentId(Integer studentid) {
        List<Exam> exams = new ArrayList<>();

        List<AttendExam> attendExams = attendExamMapper.selectByStudentId(studentid);

        for(AttendExam attendExam : attendExams){
            exams.add(examMapper.selectByPrimaryKey(attendExam.getExamid()));
        }

        return exams;
    }

    @Override
    public List<Exam> selectByCourseTeachid(Integer courseteachid) {
        return examMapper.selectByCourseTeachid(courseteachid);
    }

}
